<include file="Common/header" />
<title>财务管理</title>
</head>
<body>
<nav class="breadcrumb">
	<i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 费用管理
	<span class="c-gray en">&gt;</span> 费用收取
	<a class="btn btn-success radius r mr-20" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" >
		<i class="Hui-iconfont">&#xe68f;</i>
	</a>
</nav>
<!--基本信息-->
<div class="pd-20">
	<!--查询条件-->
	<div class="panel panel-primary">
		<div class="panel-header">按条件查询</div> 
		<div class="panel-body">
			房间号：<input class="input-text radius" id="room_number" name="room_number" style="width: 180px; " value="601" />
			合同号：<input class="input-text radius" id="contract" name="contract" style="width: 180px; " />
			<a href="javascript:;" class="btn btn-success radius querys"><i class="Hui-iconfont">&#xe709;</i> 查询</a> 
		</div>
	</div>
	<!--基本数据-->
	<div class="codeView docs-example" data-afterContent="用户基本信息">
		<table class="table table-border table-bordered">
			<tbody>
				<tr>
					<td class="text-c" bgcolor="#f1f1f1">姓名：</td>
					<td class="text-c" bgcolor="white">小白</td>
					<td class="text-c" bgcolor="#f1f1f1">合同号：</td>
					<td class="text-c" bgcolor="white">MMGY20171214001</td>
					<td class="text-c" bgcolor="#f1f1f1">房号：</td>
					<td class="text-c" bgcolor="white">601</td>
					<td class="text-c" bgcolor="#f1f1f1">签约时间：</td>
					<td class="text-c" bgcolor="white">2017-01-01 ~ 2017-12-31</td>
					<td class="text-c" bgcolor="#f1f1f1">月租：</td>
					<td class="text-c" bgcolor="white">￥1450.00</td>
					<td class="text-c" bgcolor="#f1f1f1">物业：</td>
					<td class="text-c" bgcolor="white">￥84.00</td>
					<td class="text-c" bgcolor="#f1f1f1">月租截止时间：</td>
					<td class="text-c" bgcolor="white">2017-09-01</td>
					<td class="text-c" bgcolor="#f1f1f1">物业截止时间：</td>
					<td class="text-c" bgcolor="white">2017-09-01</td>
				</tr>
				<tr>
					<td class="text-c" bgcolor="#f1f1f1">优惠活动：</td>
					<td class="text-l" bgcolor="white" colspan="15">[D1房型]签约超过12个月享受房租最后2个月(全免);</td>
				</tr>
			</tbody>
		</table>
	</div>
	<!--押金定金收取-->
	<div class="codeView docs-example" data-afterContent="押金/定金/其他收费">
		<table class="table table-border table-bordered table-hover table-bg">
			<thead>
				<tr class="text-c">
					<th width="30">选择</th>
					<th width="60">项目</th>
					<th width="200">&nbsp;</th>
					<th width="30">选择</th>
					<th width="60">项目</th>
					<th width="200">&nbsp;</th>
					<th width="30">选择</th>
					<th width="60">项目</th>
					<th width="200">&nbsp;</th>
					<th>&nbsp;</th>
				</tr>
			</thead>
			<tbody>
				<tr class="text-c">
					<td>
						<div class="check-box" style="padding-left: 0px; padding-right: 5px;">
							<input type="checkbox" attkey="deposit" class="nodeCheckbox" style="vertical-align:middle; float: left;" />
						</div>
					</td>
					<td>押金</td>
					<td class="text-l">
						<input type="input-text radius" name="deposit" id="deposit" value="0.00" class="input-text radius keyups" style="width: 150px;" readonly="true" /> 元
					</td>
					<td>
						<div class="check-box" style="padding-left: 0px; padding-right: 5px;">
							<input type="checkbox" attkey="deposit_first" class="nodeCheckbox deposit_first" style="vertical-align:middle; float: left;" />
						</div>
					</td>
					<td>定金</td>
					<td class="text-l">
						<input type="input-text radius" name="deposit_first" id="deposit_first" value="0.00" class="input-text radius keyups" style="width: 150px;" /> 元
					</td>
					<td>
						<div class="check-box" style="padding-left: 0px; padding-right: 5px;">
							<input type="checkbox" attkey="other" class="nodeCheckbox deposit_first" style="vertical-align:middle; float: left;" />
						</div>
					</td>
					<td>其他</td>
					<td class="text-l">
						<input type="input-text radius" name="other" id="other" value="0.00" class="input-text radius keyups" style="width: 150px;" /> 元
					</td>
					<th>&nbsp;</th>
				</tr>
			</tbody>
		</table>
	</div>
	<!--水电气-->
	<div class="codeView docs-example" data-afterContent="水电气费">
		<table class="table table-border table-bordered table-hover table-bg">
			<thead>
				<tr class="text-c">
					<th width="30">选择</th>
					<th width="60">项目</th>
					<th width="200">&nbsp;</th>
					<th width="30">选择</th>
					<th width="60">项目</th>
					<th width="200">&nbsp;</th>
					<th width="30">选择</th>
					<th width="60">项目</th>
					<th width="200">&nbsp;</th>
					<th class="text-l">账单月</th>
				</tr>
			</thead>
			<tbody>
				<tr class="text-c">
					<td>
						<div class="check-box" style="padding-left: 0px; padding-right: 5px;">
							<input type="checkbox" attkey="water_power" class="nodeCheckbox" style="vertical-align:middle; float: left;" />
						</div>
					</td>
					<td>水费</td>
					<td class="text-l">
						<input type="input-text radius" name="water_power" id="water_power" value="0.00" class="input-text radius keyups" style="width: 150px;" readonly="true" /> 元
					</td>
					<td>
						<div class="check-box" style="padding-left: 0px; padding-right: 5px;">
							<input type="checkbox" attkey="electric" class="nodeCheckbox electric" style="vertical-align:middle; float: left;" />
						</div>
					</td>
					<td>电费</td>
					<td class="text-l">
						<input type="input-text radius" name="electric" id="electric" value="0.00" class="input-text radius keyups" style="width: 150px;" /> 元
					</td>
					
					<td>
						<div class="check-box" style="padding-left: 0px; padding-right: 5px;">
							<input type="checkbox" attkey="gas" class="nodeCheckbox gas" style="vertical-align:middle; float: left;" />
						</div>
					</td>
					<td>气费</td>
					<td class="text-l">
						<input type="input-text radius" name="gas" id="gas" value="0.00" class="input-text radius keyups" style="width: 150px;" /> 元
					</td>
					<td class="text-l">
						<?php $lastMonth = date('Y-m', strtotime("-1 month")); list($lyear, $lmonth) = explode('-',$lastMonth); ?>
						<span class="select-box radius" style="width: 100px;">
							<select class="select" id="wyears" name="wyears">
								<?php $years = date('Y'); for($i=$years; $i>=($years-5); $i--){ ?>
								<option value="{$i}" {$i == $lyear ? 'selected' : ''}>{$i}年</option>
								<?php } ?>
							</select> 
						</span>
						<span class="select-box radius" style="width: 70px;">
							<select class="select" id="wmonths" name="wmonths">
								<?php for($k=1;$k<=12;$k++){ ?>
								<option value="{$k}" {$k == $lmonth/1 ? 'selected' : ''}>{$k}月</option>
								<?php } ?>
							</select> 
						</span> 
					</td>
				</tr>
			</tbody>
		</table>
	</div>
	<!--月租物业费-->
	<div class="codeView docs-example" data-afterContent="月租/物业费">
		<table class="table table-border table-bordered table-hover table-bg">
			<thead>
				<tr class="text-c">
					<th width="30">选择</th>
					<th width="60">项目</th>
					<th width="200">&nbsp;</th>
					<th width="30">选择</th>
					<th width="60">项目</th>
					<th width="200">&nbsp;</th>
					<th>收费月数</th>
					<th>收费时间</th>
					<th>月租截止时间</th>
					<th>物业截止时间</th>
				</tr>
			</thead>
			<tbody>
				<tr class="text-c">
					<td>
						<div class="check-box" style="padding-left: 0px; padding-right: 5px;">
							<input type="checkbox" attkey="rent" class="nodeCheckbox rent" checked />
						</div>
					</td>
					<td>房租</td>
					<td class="text-l">
						<input type="input-text radius" name="rent" id="rent" value="1450.00" class="input-text radius keyups" style="width: 100px;" readonly="true" /> 元
					</td>
					<td>
						<div class="check-box" style="padding-left: 0px; padding-right: 5px;">
							<input type="checkbox" attkey="property" class="nodeCheckbox property" checked />
						</div>
					</td>
					<td>物业费</td>
					<td class="text-l">
						<input type="input-text radius" name="property" id="property" value="84.00" class="input-text radius keyups" style="width: 100px;" readonly="true" /> 元
					</td>
					<td class="text-c">
						<span class="select-box radius" style="width: 50px;">
							<select class="select" id="monthc" name="monthc">
								<option value="1">1</option>
								<option value="2">2</option>
								<option value="3" selected>3</option>
								<option value="4">4</option>
								<option value="5">5</option>
								<option value="6">6</option>
								<option value="7">7</option>
								<option value="8">8</option>
								<option value="9">9</option>
								<option value="10">10</option>
								<option value="11">11</option>
								<option value="12">12</option>
							</select> 
						</span> 月
					</td>
					<td>2017-10-01</td>
					<td>2017-12-31</td>
					<td>2017-12-31</td>
				</tr>
				<tr>
					<td class="text-c" colspan="2">备注</td>
					<td colspan="8">
						<input type="text" name="remark" id="remark" class="input-text radius" />
					</td>
				</tr>
				<tr>
					<td class="text-c" colspan="2">合计</td>
					<td class="text-l" colspan="8">
						<input type="input-text radius" name="acount" id="acount" value="" class="input-text radius" style="width: 180px;" readonly="true" /> 元
					</td>
				</tr>
				<input type="hidden" name="begin_time" id="begin_time" class="input-text radius" value="" readonly="true" />
			</tbody>
		</table>
	</div>
	<!--缴费账单信息-->
	<div class="codeView docs-example" data-afterContent="缴费账单信息" id="costorder">
		<table class="table table-border table-bordered table-hover table-bg">
			<thead>
				<tr class="text-c">
					<th width="30">选择</th>
					<th>类型</th>
					<th>收费月份</th>
					<th>租金</th>
					<th>物业费</th>
					<th>水费</th>
					<th>电费</th>
					<th>其他</th>
					<th>应缴金额</th>
					<th>账单状态</th>
				</tr>
			</thead>
			<tbody>
				<tr class="text-c">
					<td></td>
					<td>固定</td>
					<td>2017-09</td>
					<td><p class="c-primary">1450.00</p></td>
					<td><p class="c-error">84.00</p></td>
					<td><p class="c-success">0.00</p></td>
					<td><p class="c-success">0.00</p></td>
					<td><p class="c-success">0.00</p></td>
					<td>1534.00</td>
					<td>未缴费</td>
				</tr>
				<tr class="text-c">
					<td></td>
					<td>固定</td>
					<td>2017-10</td>
					<td><p class="c-primary">1450.00</p></td>
					<td><p class="c-error">84.00</p></td>
					<td><p class="c-success">0.00</p></td>
					<td><p class="c-success">0.00</p></td>
					<td><p class="c-success">-1534.00</p></td>
					<td>0.00</td>
					<td>已缴费</td>
				</tr>
				<tr class="text-c">
					<td></td>
					<td>固定</td>
					<td>2017-10</td>
					<td><p class="c-primary">1450.00</p></td>
					<td><p class="c-error">84.00</p></td>
					<td><p class="c-success">0.00</p></td>
					<td><p class="c-success">0.00</p></td>
					<td><p class="c-success">-1534.00</p></td>
					<td>0.00</td>
					<td>已缴费</td>
				</tr>
				<tr class="text-c">
					<td><input type="checkbox" /></td>
					<td>日常</td>
					<td>2017-09</td>
					<td><p class="c-primary">0.00</p></td>
					<td><p class="c-error">0.00</p></td>
					<td><p class="c-success">10.00</p></td>
					<td><p class="c-success">10.00</p></td>
					<td><p class="c-success">0.00</p></td>
					<td>20.00</td>
					<td>未缴费</td>
				</tr>
			</tbody>
		</table>
	</div>
	<!--支付方式-->
	<div class="codeView docs-example" data-afterContent="支付方式">
		<table class="table table-border table-bordered table-striped">
			<tr>
				<td class="text-c" bgcolor="#f1f1f1">现金：</td>
				<td>
					<input type="text" name="xianjin" id="xianjin" class="input-text radius" value="0" style="width: 150px;" />
					<span class="usernameabel">元</span>
				</td>
				
				<td class="text-c" bgcolor="#f1f1f1">支付宝：</td>
				<td>
					<input type="text" name="alipay" id="alipay" class="input-text radius" value="0" style="width: 100px;" />
					<span class="usernameabel">元</span>
					
					<span style="padding-left: 20px;">支付订单号：</span>
					<input type="text" name="alipay_ordersn" id="alipay_ordersn" class="input-text radius" value="" style="width: 200px;" />
				</td>
			</tr>
			<tr>
				<td class="text-c" bgcolor="#f1f1f1">POS机：</td>
				<td>
					<input type="text" name="lakala" id="lakala" class="input-text radius" value="0" style="width: 150px;" />
					<span class="usernameabel">元</span>
				</td>
				<td class="text-c" bgcolor="#f1f1f1">微信：</td>
				<td>
					<input type="text" name="weixin" id="weixin" class="input-text radius" value="0" style="width: 100px;" />
					<span class="usernameabel">元</span>
					
					<span style="padding-left: 20px;">支付订单号：</span>
					<input type="text" name="weixin_ordersn" id="weixin_ordersn" class="input-text radius" value="" style="width: 200px;" />
				</td>
			</tr>
			<tr>
				<td class="text-c" bgcolor="#f1f1f1">转账：</td>
				<td>
					<input type="text" name="zhuanzhang" id="zhuanzhang" class="input-text radius" value="0" style="width: 150px;" />
					<span class="usernameabel">元</span>
				</td>
				<td class="text-c" bgcolor="#f1f1f1"></td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td colspan="4" class="text-c">
					<input type="button" value="生成二维码" class="btn btn-danger radius roleSubmit" style="width:100px;" />
					<input type="button" value="确认保存" class="btn btn-success radius paysuccess" style="width:100px;" />
				</td>
			</tr>
		</table>
	</div>
	<!--二维码-->
	<div class="codeView docs-example content4" style="display: none;" data-afterContent="支付二维码">
		<div style="width: 80%; overflow:hidden; text-align: center; padding-left: 100px;">
			<div style="width: 40%; float: left; margin-left: 10px; display: none;" class="weixin">
				<div class="title_right"><strong>微信</strong></div>
				<img src="" class="weixin" />
			</div>
			
			<div style="width: 40%; float: left; margin-left: 10px; display: none;" class="alipay">
				<div class="title_right"><strong>支付宝</strong></div>
				<img src="" class="alipay" />
			</div>
		</div>
	</div>
	<input type="hidden" name="rent_times" id="rent_times" />
	<input type="hidden" name="pro_times" id="pro_times" />
</div>

<script>
	var orderArrt = [];
	
	function checkNumber(number){
		var digit = /^-?\d+(\.\d+)?$/;
        if (!digit.test(number)){
            alert("只能输入负数或数字");
            inputObj.focus();
        }
	}
	
	function checkboxClick(obj, water, electric, year, month, ordertype, orderid){
		if(ordertype/1 == 2){
			if($(obj).is(':checked')){
				$("#water_power").val(water);
				$("#electric").val(electric);
				orderArrt.push(orderid);
			}else{
				$("#water_power").val(0);
				$("#electric").val(0);
				//取消选中
				for(var i in orderArrt){
					if(orderArrt[i] == orderid){
						delete orderArrt[i];
					}
				}
			}
			
			$("#wyears option").each(function(){
				if($(this).val() == year){
					$(this).prop('selected', true);
				}
			});
			
			$("#wmonths option").each(function(){
				if($(this).val() == month/1){
					$(this).prop('selected', true);
				}
			});
		}else{
			if($(obj).is(':checked')){
				$("#rent").val(water);
				$("#property").val(electric);
				orderArrt.push(orderid);
			}else{
				$("#rent").val(0);
				$("#property").val(0);
				//取消选中
				for(var i in orderArrt){
					if(orderArrt[i] == orderid){
						delete orderArrt[i];
					}
				}
			}
		}
	}
	
	$(document).ready(function(){
		var fromData = {};
		var endtime;
		var propertytime;
		//设置时间控件
		$(".time").on('click', function(){
			laydate({istime: true, format: 'YYYY-MM-DD'});
		});
		
		$(".times").on('click', function(){
			laydate({istime: true, format: 'YYYY-MM-DD hh:mm'});
		});
		
		//提交数据
		$(".roleSubmit").on('click', function(){
			var load = layer.load(2);
			if(fromData.contract){
				//获取金额输入框值
				$(".nodeCheckbox").each(function(){
					var thisKeyId = $(this).attr('attkey');
					if($(this).is(":checked")){
						fromData[thisKeyId] = parseFloat($("#"+ thisKeyId).val());
					}else{
						if(thisKeyId == 'rent' || thisKeyId == 'property'){
							fromData[thisKeyId] = 0;
						}else{
							fromData[thisKeyId] = 0;
						}
					}
				});

				var remark = $("#remark").val() || "";
				if(remark == "" || remark.length <= 0){
					layer.close(load);
					layer.msg("备注不能为空", {icon: 5, time: 2000});
					return false;
				}

				fromData['remark'] = remark;
				fromData['pay_type'] = $("#pay_type").val();
				fromData['pay_card'] = $("#pay_card").val();
				//增加收费时间
				fromData['paytime'] = $("#paytime").val();

				var begin_time = $("#begin_time").val();
				fromData['begin_time'] = begin_time;
				//判断是否收取房租
				if($(".rent").is(':checked')){
					//判断时间
					var end_time = $("#end_time").val();
					if(end_time.length <= 0){
						layer.close(load);
						layer.msg("请选择房租结束时间", {icon: 5, time: 2000});
						return false;
					}else{
						fromData['end_time'] = end_time;
					}
				}

				if($(".property").is(":checked")){
					var property_time = $("#property_time").val();
					fromData['property_time'] = property_time;
				}

				var weixinTotal = $("#weixin").val() || "",
					alipayTotal = $("#alipay").val() || "",
					xianjinTotal = $("#xianjin").val(),
					lakalaTotal = $("#lakala").val(),
					zhuanzhang = $("#zhuanzhang").val();
				//判断分配金额是否为数字
				if(!isNaN(weixinTotal) && !isNaN(alipayTotal) && !isNaN(xianjinTotal) && !isNaN(lakalaTotal) && !isNaN(zhuanzhang)){
					//判断金额是否一致
					var total = lakalaTotal/1 + weixinTotal/1 + alipayTotal/1 + xianjinTotal/1 + zhuanzhang/1;
					if(total != $("#acount").val()/1){
						layer.close(load);
						layer.msg('支付金额分配错误，请重新分配。');
						return;
					}

					fromData['xianjin'] = xianjinTotal;
					fromData['lakala'] = lakalaTotal;
					fromData['alipay'] = alipayTotal;
					fromData['weixin'] = weixinTotal;
					fromData['zhuanzhang'] = zhuanzhang;
				}else{
					layer.close(load);
					layer.msg('请正确分配支付金额比例.');
					return false;
				}
				
				fromData['lyears'] = $("#wyears").val();
				fromData['lmonth'] = $("#wmonths").val();
				fromData['month'] = $("#monthc").val();
				//添加订单信息
				$.post('ajaxcreate.html', {json: JSON.stringify(fromData)}, function(data){
					layer.close(load);
					if(data.errCode/1 == 1){
						layer.msg('操作成功', {icon: 1, time: 2000}, function(){
							if(data.weixin == '' && data.alipay == ''){
								location.reload();
							}else{
								$(".content4").show();
								if($("#weixin").val()/1){
									$("div.weixin").show();
									$("img.weixin").attr('src', "__PUBLIC__/ercode/"+ data.weixin);
								}else{
									$("div.weixin").hide();
								}
								
								if($("#alipay").val()/1){
									$("div.alipay").show();
									$("img.alipay").attr('src', "__PUBLIC__/ercode/"+ data.alipay);
								}else{
									$("div.alipay").hide();
								}
							}
							//开始查询状态
							ajax_function(data.gobackid, fromData.roomnumber, fromData.userid);
						});
					}else if(data.errCode/1 == 2){
						layer.msg('合同已到期', {icon: 5, time: 2000});
					}else{
						layer.msg('操作失败', {icon: 2, time: 2000});
					}
				});
			}else{
				//判断是否存在定金
				if($(".deposit_first").is(":checked")){
					var deposit_first = $("#deposit_first").val();
					if(deposit_first/1 > 0){
						fromData['deposit_first'] = deposit_first;
						var nickname = $("#field1").val();
						if(nickname.length <= 0 || nickname == ""){
							layer.close(load);
							layer.msg("客户姓名不能为空", {icon: 5, time: 2000});
							return false;
						}
						fromData['nickname'] = nickname;
						fromData['typeid'] = 1;

						var remark = $("#remark").val() || "";
						if(remark == "" || remark.length <= 0){
							layer.close(load);
							layer.msg("备注不能为空", {icon: 5, time: 2000});
							return false;
						}
						fromData['remark'] = remark;
						fromData['pay_type'] = $("#pay_type").val();
						fromData['pay_card'] = $("#pay_card").val();
						//增加收费时间
						fromData['paytime'] = $("#paytime").val();
						var weixinTotal = $("#weixin").val(),
							alipayTotal = $("#alipay").val(),
							xianjinTotal = $("#xianjin").val(),
							zhuanzhang = $("#zhuanzhang").val(),
							lakalaTotal = $("#lakala").val();
						//判断分配金额是否为数字
						if(!isNaN(weixinTotal) && !isNaN(alipayTotal) && !isNaN(xianjinTotal) && !isNaN(lakalaTotal) && !isNaN(zhuanzhang)){
							//判断金额是否一致
							var total = lakalaTotal/1 + weixinTotal/1 + alipayTotal/1 + xianjinTotal/1 + zhuanzhang/1;
							if(total != $("#acount").val()/1){
								layer.close(load);
								layer.msg('支付金额分配错误，请重新分配。');
								return;
							}

							fromData['xianjin'] = xianjinTotal;
							fromData['lakala'] = lakalaTotal;
							fromData['alipay'] = alipayTotal;
							fromData['weixin'] = weixinTotal;
							fromData['zhuanzhang'] = zhuanzhang;
						}else{
							layer.close(load);
							layer.msg('请正确分配支付金额比例.');
							return false;
						}

						$.post('ajaxcreate.html', {json: JSON.stringify(fromData)}, function(data){
							layer.close(load);
							if(data.errCode/1 == 1){
								layer.msg('操作成功', {icon: 1, time: 2000}, function(){
									if(data.weixin == '' && data.alipay == ''){
										location.reload();
									}else{
										$(".content4").show();
										if($("#weixin").val()/1){
											$("div.weixin").show();
											$("img.weixin").attr('src', "__PUBLIC__/ercode/"+ data.weixin);
										}else{
											$("div.weixin").hide();
										}
										
										if($("#alipay").val()/1){
											$("div.alipay").show();
											$("img.alipay").attr('src', "__PUBLIC__/ercode/"+ data.alipay);
										}else{
											$("div.alipay").hide();
										}
									}
									//开始查询状态
									ajax_function(data.gobackid, fromData.roomnumber, fromData.userid);
								});
							}else if(data.errCode/1 == 2){
								layer.msg('合同已到期', {icon: 5, time: 2000});
							}else{
								layer.msg('操作失败', {icon: 2, time: 2000});
							}
						});
					}else{
						layer.msg(load);
						layer.msg('请点击确认保存按钮', {icon: 5, time: 2000});
					}
				}else{
					layer.close(load);
					layer.msg('请先查询目标房间数据', {icon: 5, time: 2000});
				}
			}
		});
		
		//确定保存
		$(".paysuccess").on('click', function(){
			if(fromData.contract){
				var load = layer.load(2);
				
				//获取金额输入框值
				$(".nodeCheckbox").each(function(){
					var thisKeyId = $(this).attr('attkey');
					if($(this).is(":checked")){
						fromData[thisKeyId] = parseFloat($("#"+ thisKeyId).val());
					}else{
						if(thisKeyId == 'rent' || thisKeyId == 'property'){
							fromData[thisKeyId] = 0;
						}else{
							fromData[thisKeyId] = 0;
						}
					}
				});

				var remark = $("#remark").val() || "";
				if(remark == "" || remark.length <= 0){
					layer.close(load);
					layer.msg("备注不能为空", {icon: 5, time: 2000});
					return false;
				}
				fromData['remark'] = remark;

				fromData['pay_type'] = $("#pay_type").val();
				fromData['pay_card'] = $("#pay_card").val();
				//增加收费时间
				fromData['paytime'] = $("#paytime").val();
				var begin_time = $("#begin_time").val();
				fromData['begin_time'] = begin_time;
				//判断是否收取房租
				if($(".rent").is(':checked')){
					//判断时间
					var end_time = $("#end_time").val();
					if(end_time.length <= 0){
						layer.close(load);
						layer.msg("房租结束时间错误", {icon: 5, time: 2000});
						return false;
					}else{
						fromData['end_time'] = end_time;
					}
				}

				if($(".property").is(":checked")){
					var property_time = $("#property_time").val();
					fromData['property_time'] = property_time;
				}

				var weixinTotal = $("#weixin").val(),
					alipayTotal = $("#alipay").val(),
					xianjinTotal = $("#xianjin").val(),
					lakalaTotal = $("#lakala").val(),
					zhuanzhang = $("#zhuanzhang").val();
				//判断分配金额是否为数字
				if(!isNaN(weixinTotal) && !isNaN(alipayTotal) && !isNaN(xianjinTotal) && !isNaN(lakalaTotal) && !isNaN(zhuanzhang)){
					//判断金额是否一致
					var total = lakalaTotal/1 + weixinTotal/1 + alipayTotal/1 + xianjinTotal/1 + zhuanzhang/1;
					if(total != $("#acount").val()/1){
						layer.close(load);
						layer.msg('支付金额分配错误，请重新分配。');
						return;
					}

					fromData['xianjin'] = xianjinTotal;
					fromData['lakala'] = lakalaTotal;
					fromData['alipay'] = alipayTotal;
					fromData['weixin'] = weixinTotal;
					fromData['zhuanzhang'] = zhuanzhang;
					fromData['alipay_ordersn'] = $("#alipay_ordersn").val();
					fromData['weixin_ordersn'] = $("#weixin_ordersn").val();
					fromData['lyears'] = $("#wyears").val();
					fromData['lmonth'] = $("#wmonths").val();
					fromData['month'] = $("#monthc").val();
				}else{
					layer.close(load);
					layer.msg('请正确分配支付金额比例.');
					return false;
				}

				$.post('ajaxcreate2.html', {json: JSON.stringify(fromData)}, function(data){
					layer.close(load);
					if(data.errCode/1 == 1){
						layer.msg('操作成功',{icon: 1, time: 2000}, function(){
							//刷新网页
							location.reload();
						});
					}else if(data.errCode/1 == 2){
						layer.msg('合同已到期', {icon: 5, time: 2000});
					}else{
						layer.msg('操作失败',{icon: 1, time: 2000});
					}
				});
			}else{
				if($(".deposit_first").is(":checked")){
					var deposit_first = $("#deposit_first").val();
					if(deposit_first/1 != 0){
						fromData['deposit_first'] = deposit_first;
						
						var nickname = $("#field1").val();
						if(nickname.length <= 0 || nickname == ""){
							layer.close(load);
							layer.msg("客户姓名不能为空", {icon: 5, time: 2000});
							return false;
						}
						
						fromData['nickname'] = nickname;
						if(deposit_first/1 > 0){
							fromData['typeid'] = 1;
						}else{
							fromData['typeid'] = 0;
						}

						var remark = $("#remark").val() || "";
						if(remark == "" || remark.length <= 0){
							layer.close(load);
							layer.msg("备注不能为空", {icon: 5, time: 2000});
							return false;
						}
						fromData['remark'] = remark;
						fromData['pay_type'] = $("#pay_type").val();
						fromData['pay_card'] = $("#pay_card").val();
						//增加收费时间
						fromData['paytime'] = $("#paytime").val();
						var weixinTotal = $("#weixin").val(),
							alipayTotal = $("#alipay").val(),
							xianjinTotal = $("#xianjin").val(),
							zhuanzhang = $("#zhuanzhang").val(),
							lakalaTotal = $("#lakala").val();
						//判断分配金额是否为数字
						if(!isNaN(weixinTotal) && !isNaN(alipayTotal) && !isNaN(xianjinTotal) && !isNaN(lakalaTotal) && !isNaN(zhuanzhang)){
							//判断金额是否一致
							var total = lakalaTotal/1 + weixinTotal/1 + alipayTotal/1 + xianjinTotal/1 + zhuanzhang/1;
							if(total != $("#acount").val()/1){
								layer.close(load);
								layer.msg('支付金额分配错误，请重新分配。');
								return;
							}

							fromData['xianjin'] = xianjinTotal;
							fromData['lakala'] = lakalaTotal;
							fromData['alipay'] = alipayTotal;
							fromData['weixin'] = weixinTotal;
							fromData['zhuanzhang'] = zhuanzhang;
						}else{
							layer.close(load);
							layer.msg('请正确分配支付金额比例.');
							return false;
						}

						$.post('ajaxcreate2.html', {json: JSON.stringify(fromData)}, function(data){
							layer.close(load);
							if(data.errCode/1 == 1){
								layer.msg('操作成功',{icon: 1, time: 2000}, function(){
									//刷新网页
									location.reload();
								});
							}else if(data.errCode/1 == 2){
								layer.msg('合同已到期', {icon: 5, time: 2000});
							}else{
								layer.msg('操作失败',{icon: 1, time: 2000});
							}
						});
					}
				}else{
					layer.close(load);
					layer.msg('请先查询目标房间数据');
				}
			}
		});
		
		var uinfo = {};
		//查询数据
		$(".querys").on('click', function(){
			//重置数据
			orderArrt = [];
			var where = {};
				where.btime = ""; //$("#btime").val();
				where.etime = "";//$("#etime").val();
				where.roomnumber = $("#room_number").val();
				where.contract = $("#contract").val();
			var load = layer.load(2);
			$.get('ajaxcreate.html', where, function(data){
				layer.close(load);
				if(data.errCode/1){
					fromData = data.info;
					//将所有选中设置为未选中
					$(".nodeCheckbox").each(function(){
						$(this).attr('checked', false);
					});
					//清空总计
					$("#acount").val("0.00");
					//Set Value
					$("#deposit").val(fromData.deposit);
					$("#deposit_first").val(fromData.deposit_first);
					$("#water_power").val(fromData.water_power);
					$("#electric").val(fromData.electric);
					$("#property").val(fromData.property);
					$("#rent").val(fromData.rent);
					$("#card_peposit").val(fromData.card_peposit);
					//$("#acount").val(fromData.acount);
					$("#other").val(fromData.other);
					//加载会员详情
					uinfo = data.field;
					$("#field2").val(uinfo.contract);
					$("#field1").val(uinfo.nickname);
					//费用截止时间
					$("#field3").val(uinfo.propertytime);
					$("#field4").val(uinfo.endtime);
					$("#begin_time").val(uinfo.begintime);
					
					var totalMonth = uinfo.month/1+1;
					//设置费用结束时间
					/*
					var rentEndDate =  c_strtotime(uinfo.btime, totalMonth, true);
					var propertyMonth = uinfo.pmonth/1;
					var proEndDate = c_strtotime(uinfo.btime, (propertyMonth+1), true);
					*/
					$("#end_time").val(uinfo.bbtime);
					$("#rent_times").val(uinfo.bbtime);
					//设置物业费用结束时间
					$("#property_time").val(uinfo.pptime);
					$("#pro_times").val(uinfo.pptime);
					//加载未缴费账单
					$("#costorder").html(data.order);
					//加载日志
					$("#ajaxDiv").html(data.html);
				}else{
					layer.msg('未查询到相关信息');
				}
			});
		});
		
		$(".nodeCheckbox").on('change', function(){
			var obj = $(this);
			var attkey = obj.attr('attkey');
			var acount = $("#acount").val()/1;
			var ncount = $("#"+ attkey).val()/1;
			
			if(obj.is(':checked')){
				acount += ncount;
			}else{
				acount -= ncount;
			}
			
			$("#acount").val(acount.toFixed(2));
		});
		
		$(".keyups").on('keyup', function(event){
			var vals = $(this).val() || 0;
			//1. 判断是否为数字
			if(!isNaN(vals)){
				var acount = 0;
				//获取所有选中的checkbox
				$(".nodeCheckbox").each(function(e){
					if($(this).is(":checked")){
						var thisCountId = $(this).attr('attkey');
						var thisCountVal = $("#"+ thisCountId).val();
						acount += parseFloat(thisCountVal);
					}
				});

				$("#acount").val(acount.toFixed(2));
			}
		});

		var amonth=1;
		//月数选择
		$("#monthc").on('change', function(){
			if(fromData.rent){
				var month = $(this).val()/1 || 1;
				if(month > 1){
					
				}else{
					month = 1;
				}
				var rent = fromData.rent*month;
				var property = fromData.property*month;
				$("#rent").val(rent.toFixed(2));
				$("#property").val(property.toFixed(2));
				var totalMonth = month + uinfo.month/1;
				//获取月租结束时间
				var rentEndDate = ajax_strtotime(uinfo.btime, totalMonth, true, true); //c_strtotime(uinfo.btime, totalMonth, true);
				//设置物业费用结束时间
				var propertyMonth = month + uinfo.pmonth/1;
				var proEndDate = ajax_strtotime(uinfo.btime, propertyMonth, true, false); //c_strtotime(uinfo.btime, (propertyMonth+month), true);
				
				if($(".rent").is(":checked") || $(".property").is(":checked")){
					var xmonth = Math.abs(month - amonth);
					var rent = xmonth * fromData.rent;
					var property = xmonth * fromData.property;

					if($(".rent").is(':checked') && $(".property").is(":checked")){ //月租费项选中
						var mcount = rent + property;
					}else if($(".rent").is(":checked") && !$(".property").is(":checked")){
						var mcount = rent;
					}else if(!$(".rent").is(":checked") && $(".property").is(":checked")){
						var mcount = property;
					}
					//判断是减还是加
					if(month > amonth){
						var acount = $("#acount").val()/1 + mcount;
					}else{
						var acount = $("#acount").val()/1 - mcount;
					}

					$("#acount").val(acount.toFixed(2));
				}
				amonth = month;
			}else{
				layer.msg('请先查询房间信息');
			}
		});
	});
	
	var ajax_function = function(gobackid, roomnumber, uuid){
		$.get('ajaxcreateresult.html',{gobackid: gobackid, roomnumber: roomnumber, uuid: uuid}, function(data){
			if(data.errCode/1){
				$("#ajaxDiv").html(data.html);
			}
			
			setTimeout(function(){
				ajax_function(gobackid, roomnumber, uuid);
			}, 10000);
		});
	}
	
	/**
	 * 远程计算时间
	 **/
	var ajax_strtotime = function(btime, month, isac, isrent){
		$.get('ajaxtime.html', {btime: btime, month: month, isac: isac}, function(time){
			if(isrent){
				$("#end_time").val(time);
			}else{
				$("#property_time").val(time);
			}
		});
	}
	
	/**
	 * 计算月份
	 * @param string btime 开始时间
	 * @param int month 月数
	 **/
	var c_strtotime = function(btime, month, isac) {
		var beginAttr = btime.split('-');
		var date = new Date(beginAttr[0], (beginAttr[1]/1 - 1), 1);
			//添加月
			date.setMonth(date.getMonth()+month); //时间添加N个月
		//开始格式时间
		var rentEndDate = date.toLocaleDateString().replace(/\//g, '-').replace(/(年|月|日)/g, '-').replace(/-$/gi, '');
		var endAttr = rentEndDate.split('-');
		var endtime = new Date(endAttr[0], endAttr[1], 0);
		//日期
		var beiginDay = beginAttr[2]/1;
		//获取结果月最大天数
		var maxDay = endtime.getDate();
		//判断起始时间是否是2月
		if(beginAttr[1]/1 == 2){
			var dates = new Date(beginAttr[0], (beginAttr[1]/1), 0);
			var maxDay2 = dates.getDate();
			if((maxDay2 - beiginDay) <= 1){ //2月最后一天
				if(isac){
					beiginDay = maxDay;
				}else{
					beiginDay = maxDay - 1;
				}
			}
		}
		
		if(isac){
			beiginDay = beiginDay - 1;
			if(!beiginDay){
				var newbegintime = new Date(endAttr[0]/1, (endAttr[1]/1-1), 0);
				//alert('--'+ newbegintime);
					newbegintime.setDate(newbegintime.getDate()-1);
				return newbegintime.toLocaleDateString().replace(/\//g, '-').replace(/(年|月|日)/g, '-').replace(/-$/gi, '');	
			}
		}
		
		if(maxDay < beiginDay){
			beiginDay = maxDay;
		}
		
		beiginDay = beiginDay/1 < 10 ? '0'+ beiginDay : beiginDay;
		return endAttr[0]+ '-' + endAttr[1] + '-' + beiginDay;
	}
</script>
</body>
</html>